package com.sxm.yiwei.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.activitis.AppManager;
import com.sxm.yiwei.activitis.BookContentHelpActivity;
import com.sxm.yiwei.tools.ErrType;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by KuKa on 2017/9/15.
 * 订单状态变化类
 */

public class BookStateChangeUtils {

    private SharedPreferences sharedPreferences;
    public String appId;
    public String userId;
    public String token;

    public void getParameter(){

        sharedPreferences = AppManager.getAppManager().currentActivity().getSharedPreferences("userlogin",Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");

    }

    //订单完结
    public  void overBook(String id) {
        getParameter();
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.BOOKOVER);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                AppManager.getAppManager().currentActivity().finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(AppManager.getAppManager().currentActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 取消订单
    public  void cancelBook(String id) {
        getParameter();
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.CANCELBOOK);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(AppManager.getAppManager().currentActivity(), "订单已取消", Toast.LENGTH_SHORT).show();
                AppManager.getAppManager().currentActivity().finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(AppManager.getAppManager().currentActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
